<?php

namespace App\Http\Controllers;

use App\Jobs\AppointList;
use App\Jobs\EduJob;
use App\Jobs\InterviewRemindJob;
use App\Jobs\TempPush;
use App\Models\Appoint;
use App\Models\InterviewRemind;
use App\Models\TempPushRecord;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class SendTempController extends Controller
{
    public function AppointQueue(Request $request){
        if(!$request->input('id')){
            return new JsonResponse(['code'=>500,'msg'=>'失败，id参数不正确'],500);
        }
        $id = $request->input('id');
        $appoint = Appoint::query()->where('id',$id)->first();
        if(!$appoint){
            return new JsonResponse(['code'=>500,'msg'=>'失败，id不存在'],500);
        }
        try {
            AppointList::dispatch($appoint);
            return new JsonResponse(['code'=>200,'msg'=>'成功'],200);
        }catch (\Exception $exception){
            return new JsonResponse(['code'=>500,'msg'=>$exception->getMessage()],500);
        }
    }

    public function AppointQueueAll(Request $request){
        if(!$request->input('id')){
            return new JsonResponse(['code'=>500,'msg'=>'失败，id参数不正确'],500);
        }
        try {

            $ids = $request->input('id');
            foreach($ids as $k=>$v){
                $appoint = Appoint::query()->where('id',$v)->where('status',6)->first();
                if($appoint) {
                    AppointList::dispatch($appoint);
                }
            }
            return new JsonResponse(['code'=>200,'msg'=>'成功'],200);
        }catch (\Exception $exception){
            return new JsonResponse(['code'=>500,'msg'=>$exception->getMessage()],500);
        }
    }


    public function InterviewRemind(Request $request)
    {
        $id = $request->input('id');
        if(!$id){
            return new JsonResponse(['code'=>500,'msg'=>'失败，id参数不正确'],500);
        }
        try {
            InterviewRemindJob::dispatch($id);
            return new JsonResponse(['code'=>200,'msg'=>'成功'],200);
        }catch (\Exception $exception){
            return new JsonResponse(['code'=>500,'msg'=>$exception->getMessage()],500);
        }
    }

    public function FamilyEdu(Request $request)
    {
        $id = $request->input('id');
        if(!$id){
            return new JsonResponse(['code'=>500,'msg'=>'失败，id参数不正确'],500);
        }
        try {
            EduJob::dispatch($id);
            return new JsonResponse(['code'=>200,'msg'=>'成功'],200);
        }catch (\Exception $exception){
            return new JsonResponse(['code'=>500,'msg'=>$exception->getMessage()],500);
        }

    }

    public function AppointRemind(Request $request){
        if(!$request->input('id')){
            return new JsonResponse(['code'=>500,'msg'=>'失败，id参数不正确'],500);
        }
        try {
            $ids = $request->input('id');
            foreach($ids as $k=>$v){
                $appoint = Appoint::query()
                    ->where('id',$v)
                    ->where('appoint_date',date('Y-m-d'))
                    ->first();
                if($appoint) {
                    $tempPushRecord = TempPushRecord::createTemp($appoint->user_id,6,$v);
                    if($tempPushRecord) {
                        TempPush::dispatch($tempPushRecord, $appoint);
                    }
                }
            }
            return new JsonResponse(['code'=>200,'msg'=>'成功'],200);
        }catch (\Exception $exception){
            return new JsonResponse(['code'=>500,'msg'=>$exception->getMessage()],500);
        }
    }
}
